package org.article;

import processing.core.*;

// TODO: REVISAR que la Section no se encargue de colorearse ni nada, que eso sea a través de la Clase artículo. 

public class Section {
	// From the content itself
	private String content;
	private int contentColor;

	// positionX, positionY, and the width of this section is determined in the document class
	private float sectionX;
	private float sectionY;
	private float sectionW;
	private float sectionH;	// The height is an attribute calculated by the Article

	// If the section is currently with the mouse over or it is selected, this flag changes
	private boolean withMouseOver;
	
	// Variable that helps with the movable sections (From the SourceArticle)
	private boolean movable;
	
	// Variable that helps with the movable sections (From the SourceArticle)
	private boolean isTranscluded;
	
	// If the section was selected in the document, this flag changes
	private boolean isCurrentlyTranscluding;
	
	// Variable that controls (overrides) the highlighting status of this section (true highlight, false not)
	private boolean enableHighlight;
	private boolean forceHighlight;	// This variable overrides the highlighting

	// This is the 'parent' section, where this seciton was transcluded
	private Section parentSection;
	
	// Variable that helps when the rectangle is on the section
	private boolean rectangleSensitivity;
	
	// Variable just for calculate the sensitivity
	private int sensitivityPadding;
		
	// The parent object (PApplet)
	private PApplet parent;
	
	// The parent PGraphics
	private PGraphics pgDocument;
	private PGraphics pgPreview;

	/**
	 * Constructor
	 * @param newContent
	 * @param newContentColor
	 * @param parent: PApplet
	 * @param pgParent: PGraphics
	 * @param pgPreview: PGraphics
	 */
	public Section(String newContent, int newContentColor, PApplet newParent, PGraphics newPgParent, PGraphics newPgPreview) {
		// Sets variables defaults (null for objects, etc)
		initializeParameters();
		
		content = newContent;
		contentColor = newContentColor;		
		
		parent = newParent;
		pgDocument = newPgParent;
		pgPreview = newPgPreview;
	}

	/**
	 * Default constructor: Just with content.
	 * The objects and flags are basically initialized
	 * @param newContent
	 */
	public Section(String newContent) {
		content = newContent;

		initializeParameters();
	}
	
	private void initializeParameters(){
		contentColor = 0;
		
		withMouseOver = false;
		isCurrentlyTranscluding = false;
		enableHighlight = true;
		forceHighlight = false;
		
		parent = null;
		pgDocument = null;
		pgPreview = null;
	}

	/**
	 * Method that draws this section.
	 * 		The position is calculated with these rules:
	 * 			posX = posXDocument + padding
	 * 
	 * 			posY0 = posYDocument + padding
	 * 			posYN = posY(N-1) + padding
	 * 		THE POSITIONING RULES ARE USED IN THE DOCUMENT CLASS!!!!!
	 */
	public void draw(){
		// Drawing color. It changes when the section is pressed
		int drawColor = contentColor;
		
		// Checks, before painting, if the mouse is over this section
		withMouseOver = checkForMouseOver();

		// Checks for pressed section
		if(isCurrentlyTranscluding){
			drawColor = parent.color(255, 0, 0);
		}
		
		// Sets the color of the section
		if(withMouseOver){
			if( !isCurrentlyTranscluding ){ // DO NOT Highlight the 'selected' section
				if( enableHighlight ){
					// TODO: Check how to change brightness
					pgDocument.fill(parent.color(drawColor + 0x555));
				}
				else{
					pgDocument.fill(drawColor);
				}
			}
			else{
				pgDocument.fill(drawColor);
			}
		}
		else{
			if(forceHighlight){
				if(enableHighlight){
					pgDocument.fill(parent.color(drawColor + 0x555));
				}
				else{
					pgDocument.fill(drawColor);
				}
			}
			else{
				pgDocument.fill(drawColor);
			}
		}
		
		// The rectangle over the document is drawn		
		pgDocument.stroke(64);
		pgDocument.rect (sectionX, sectionY, sectionW, sectionH);
		pgDocument.stroke(0);
	}
	
	/**
	 * Method that sets the size and position of this section
	 */
	public void setSectionDimensions(float sectionX2, float sectionY2, float sectionW2, float sectionH2) {
		sectionX = sectionX2;
		sectionY = sectionY2;
		sectionW = sectionW2;
		sectionH = sectionH2;
	}
	
	/**
	 * Method that sets the size and position of this section
	 */
	public float getSectionSize() {
		// TODO check when the section was transcluded
		return sectionH;
	}
	
	/**
	 * Method that sets the section color
	 * @param newColor
	 */
	public void setSectionColor(int newColor) {
		contentColor =  newColor;
	}

	/**
	 * Method that checks if this section has a mouse over, SectionX, Y and width are the dimensions of the Section
	 * @return isOver: true if it is, otherwise false
	 */
	public boolean checkForMouseOver(){
		boolean isOver = false;
		
		int posX, posY;
		
		posX = parent.mouseX;
		posY = parent.mouseY;
		
		if(sectionX < posX){
			if((sectionX + sectionW) > posX){ 
				// It is inside X axis
				if(sectionY < posY){
					if((sectionY + sectionH) > posY){
						// It is in Y axis too
						isOver = true;
					}
				}
			}
		}
		
		// It is checked if the rectangle is existent
		if(rectangleSensitivity){
			isOver = true;
			
			if(sectionX > (posX + (sensitivityPadding * 3))){
				isOver = false;
			}
			
			if((sectionX + sectionW) < (posX - (sensitivityPadding * 3))){
				isOver = false;
			}
			
			if(sectionY > (posY + (sensitivityPadding * 2))){
				isOver = false;
			}
			
			if((sectionY + sectionH) < (posY - (sensitivityPadding * 2))){
				isOver = false;
			}
		}
			
		return isOver;
	}
	
	/**
	 * Method that prints on console the content of the section
	 */
	public String getSectionText(){
		return content;
	}

	/**
	 * Method that changes the color of the selected section, and creates a 'rectangle' to drag and drop (IN DRAW METHOD)
	 */
	public void selectSection() {
		isCurrentlyTranscluding = true;
	}

	/**
	 * Method that copies the selected section into a new section
	 * @return Section: the new copied section.
	 */
	public Section releaseSection() {
		// The section is un-selected
		isCurrentlyTranscluding = false;
		
		// The new Section is created
		Section tempSection = new Section(content, contentColor, parent, pgDocument, pgPreview);

		return tempSection;
	}
	
	/**
	 * Getter method
	 * @return true or false, if is selected or not
	 */
	public boolean isSelected(){
		return isCurrentlyTranscluding;
	}
	
	/**
	 * Method that sets the highlight status of this section.
	 * @param status: 	true, allows highlighting when the mouse is over
	 * 					false, does not allow highlighting
	 */
	public void setHighlightStatus(boolean newStatus){
		enableHighlight = newStatus;
	}

	/**
	 * Method that increases the sensitivity of the section with a mouse over (now working with a rectangle around the mouse)
	 * @param sensitivity: true, enables sensitivity, false, not.
	 * @param newSensitivityPadding the padding of the document, used for the size of the rectangle.
	 */
	public void setRectangleSensitivity(boolean newSensitivity, int newSensitivityPadding) {
		rectangleSensitivity = newSensitivity;
		sensitivityPadding = newSensitivityPadding;
	}
	
	public void setMovable(boolean newMovable){
		movable = newMovable;
	}
	
	public boolean isMovable(){
		return movable;
	}

	public void setTranscluded(boolean newIsTranscluded, Section newParent) {
		this.isTranscluded = newIsTranscluded;
		
		if(isTranscluded){
			parentSection = newParent;
		}
		else{
			parentSection = null;
		}
	}

	public boolean isTranscluded() {
		return isTranscluded;
	}
	
	public void forceHighlight(boolean mustHighlight){
		forceHighlight = mustHighlight;
	}
	
	public Section getSection(){
		return this;
	}
	
	public Section getParentSection(){
		Section tempSection = null;
		
		if(isTranscluded){
			tempSection = parentSection;
		}
		
		return tempSection;
	}

	public float calculateSectionHeight() {
		return content.length();
	}

	public void setPAppletAndPGraphics(PApplet newParent, PGraphics newPgDocument, PGraphics newPgPreview) {
		parent = newParent;
		pgDocument = newPgDocument;
		pgPreview = newPgPreview;
	}
}